﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public class PrimaryKeyDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public PrimaryKeyDAL()
         { 
         
         }
        public PrimaryKeyInfo GetFieldDutyPK(Nullable<int> FieldSeq, Nullable<int> ClassSeq)
         {
             string strSQL;
             PrimaryKeyInfo info = new PrimaryKeyInfo();

             try
             {
                 dbManager.Open();
                 dbManager.CreateParameters(3);

                 dbManager.AddParameters(0, "@FieldSeq", FieldSeq);
                 dbManager.AddParameters(1, "@ClassSeq", ClassSeq);
                 dbManager.AddParameters(2, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                 strSQL = "SELECT FIELD_DUTY_SEQ"
                     + " FROM HRM_MP_FIELD_DUTIES"
                     + " WHERE RECORD_STATUS not like @DeleteRecord"
                         + " AND FIELD_POS_SEQ = @FieldSeq"
                         + " AND CLASS_SEQ = @ClassSeq";

                 IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                 while (rdr.Read())
                 {
                     info.Seq = rdr.IsDBNull(0) ? (int?)null : rdr.GetInt32(0);
                 }

                 return info;
             }
             catch
             {
                 return null;
                 throw;
             }
             finally
             {
                 dbManager.CloseReader();
                 dbManager.Dispose();
             }
         }
        public PrimaryKeyInfo GetFieldSpecPK(Nullable<int> FieldSeq, Nullable<int> ClassSeq)
        {
            string strSQL;
            PrimaryKeyInfo info = new PrimaryKeyInfo();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@FieldSeq", FieldSeq);
                dbManager.AddParameters(1, "@ClassSeq", ClassSeq);
                dbManager.AddParameters(2, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT FIELD_SPEC_SEQ"
                    + " FROM HRM_MP_FIELD_SPECIFICATIONS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                        + " AND FIELD_POS_SEQ = @FieldSeq"
                        + " AND CLASS_SEQ = @ClassSeq";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    info.Seq = rdr.IsDBNull(0) ? (int?)null : rdr.GetInt32(0);
                }

                return info;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
    }
}
